package com.cloud.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Component;

import com.cloud.core.BaseDao;
import com.cloud.dao.ContactBean;
import com.cloud.dao.UserBean;
import com.cloud.util.CommUtil;

/**
 * 1Cloud1
 * @author Administrator
 * @since 2015-11-4
 * @version 1.0 
 */
@Component("UserDao")
public class UserDao extends BaseDao {
	
	/**
	 * 查询用户
	 * @param bean
	 * @return
	 */
	public List<UserBean> getUserList(UserBean bean){
		SqlSession session = super.getInstance(true);
		List<UserBean> beanList = null;
		try {
			beanList = session.selectList("user.list", bean);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
		return beanList;
	}
	
	/**
	 * 新增用户
	 * @param bean
	 */
	public int addUser(UserBean bean){
		SqlSession session = super.getInstance(true);
		session.insert("user.insert", bean);
		session.close();
		return CommUtil.isEmpty(bean.getUserId()) ? -1 : Integer.valueOf(bean.getUserId());
	}
	
	/**
	 * 修改用户
	 * @param bean
	 * @param flag
	 */
	public void updateUser(UserBean bean, boolean flag){
		SqlSession session = super.getInstance(true);
		if(flag)
			session.update("user.updateUser", bean);
		else
			session.update("user.resetpwd", bean);
		session.close();
	}
	
	/**
	 * 查询用户联系人
	 * @param bean
	 * @param isNotice
	 * @return
	 */
	public List<ContactBean> getUserContactList(ContactBean bean, boolean isNotice){
		SqlSession session = super.getInstance(true);
		List<ContactBean> beanList = null;
		try {
			if(isNotice)
				beanList = session.selectList("user.listContNotice", bean);
			else
				beanList = session.selectList("user.listCont", bean);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
		return beanList;
	}
	
	/**
	 * 新增联系人
	 * @param bean
	 */
	public void addUserContact(ContactBean bean){
		SqlSession session = super.getInstance(true);
		try {
			session.insert("user.insertCont", bean);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			session.close();
		}
	}
	
	/**
	 * 修改联系人
	 * @param bean
	 */
	public void updateUserContact(ContactBean bean){
		SqlSession session = super.getInstance(true);
		session.update("user.updateCont", bean);
		session.close();
	}
	
	public Integer contactsCont(ContactBean bean){
		SqlSession session = super.getInstance(true);
		Integer count = session.selectOne("user.contactsCont", bean);
		session.close();
		return count;
	}
}
